package com.cloudkeeper.leasing.identity.service.impl;

import com.cloudkeeper.leasing.base.repository.BaseRepository;
import com.cloudkeeper.leasing.base.service.impl.BaseServiceImpl;
import com.cloudkeeper.leasing.identity.domain.DetectionIndex;
import com.cloudkeeper.leasing.identity.domain.KPITownQuota;
import com.cloudkeeper.leasing.identity.domain.KPIVillageQuota;
import com.cloudkeeper.leasing.identity.domain.KpiQuota;
import com.cloudkeeper.leasing.identity.dto.detectionindex.DetectionIndexSearchable;
import com.cloudkeeper.leasing.identity.dto.kpievaluation.KPIEvaluationSearchable;
import com.cloudkeeper.leasing.identity.repository.KPIVillageQuotaRepository;
import com.cloudkeeper.leasing.identity.service.*;
import com.cloudkeeper.leasing.identity.vo.KPIAttachmentVO;
import com.cloudkeeper.leasing.identity.vo.KPIEvaluationVO;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Nonnull;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 村考核指标 service
 * @author yujian
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class KPIVillageQuotaServiceImpl extends BaseServiceImpl<KPIVillageQuota> implements KPIVillageQuotaService {

    /** 村考核指标 repository */
    private final KPIVillageQuotaRepository kPIVillageQuotaRepository;

    /* 二级指标service */
    private final KPITownQuotaService kpiTownQuotaService;

    //监测指标
    private final DetectionIndexService detectionIndexService;

    //综合评价
    private final KPIEvaluationService kpiEvaluationService;

    private final KPIAttachmentService kpiAttachmentService;
    private final KpiQuotaService kpiQuotaService;

    public final FdfsService fdfsService;
    @Override
    protected BaseRepository<KPIVillageQuota> getBaseRepository() {
        return kPIVillageQuotaRepository;
    }

    @Override
    public ExampleMatcher defaultExampleMatcher() {
        return super.defaultExampleMatcher()
                .withMatcher("weight", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("districtName", ExampleMatcher.GenericPropertyMatchers.contains());
    }

    @Override
    public void deleteAllByParentDistrictIdAndParentQuotaId(String parentDistrictId, String parentQuotaId) {
        kPIVillageQuotaRepository.deleteAllByParentDistrictIdAndParentQuotaId(parentDistrictId,parentQuotaId);
    }

    @Override
    public List<Map<String, Object>> buildCommonWorkData(String districtId, String taskId, String reviewTaskId, String parentQuotaId, String quarter, String makeQuotaDistrictId) {
        List<KPITownQuota> townQuota = getKPITownQuota(districtId, makeQuotaDistrictId, taskId, parentQuotaId);
        if (townQuota.size() == 0) {
            return new ArrayList<>();
        }
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(KPIVillageQuota.class);
        detachedCriteria.add(Restrictions.in("townQuotaId", townQuota.stream().map(KPITownQuota::getId).collect(Collectors.toList())));
        if (!StringUtils.isEmpty(quarter)) {
            detachedCriteria.add(Restrictions.eq("quarter", quarter));
        }
        detachedCriteria.add(Restrictions.eq("districtId", districtId));
        List<KPIVillageQuota> villageQuotas = findAll(detachedCriteria);
        Map<String, Map<String, String>> map = new HashMap<>();
//        for (KPIVillageQuota item : villageQuotas) {
//            String quotaName = item.getKpiTownQuota().getQuotaName();
//            if (StringUtils.isEmpty(quotaName) || StringUtils.isEmpty(item.getQuarter())) {
//                continue;
//            }
//            if (!map.containsKey(quotaName)) {
//                map.put(quotaName, new HashMap<>());
//            }
//            Map<String, String> quotas = map.get(quotaName);
//            quotas.put(item.getQuarter(), item.getScore());
//        }

        ArrayList<KPITownQuota> thirdCollect = townQuota.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(KPITownQuota::getQuotaName))), ArrayList::new));
        Map<String, List<Map<String, Object>>> thirdRes = new HashMap<>();
        for (KPITownQuota item : thirdCollect) {
            String parentQuotaId1 = item.getParentQuotaId();
            if (!thirdRes.containsKey(parentQuotaId1)) {
                thirdRes.put(parentQuotaId1, new ArrayList<>());
            }
            Map<String, Object> val = new HashMap<>();
            val.put("quotasName", item.getQuotaName());
            val.put("quotaScore", item.getScore());
            val.put("quarters", map.get(item.getQuotaName()));
            thirdRes.get(parentQuotaId1).add(val);
        }

        ArrayList<KPITownQuota> secondCollect = thirdCollect.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(KPITownQuota::getParentQuotaId))), ArrayList::new));
        List<Map<String, Object>> secondRes = new ArrayList<>();
        for (KPITownQuota item : secondCollect) {
            Map<String, Object> val = new HashMap<>();
            val.put("quotasName", item.getParentQuotaName());
            val.put("quotaScore", item.getScore());
            val.put("attachment", kpiAttachmentService.findAllByQuota(item.getParentQuotaId(), item.getDistrictId()));
            val.put("thirdRes", thirdRes.get(item.getParentQuotaId()));
            secondRes.add(val);
        }

        return secondRes;
    }

    @Override
    public List<Map<String, Object>> buildCommonData(String districtId, String taskId, String reviewTaskId, String rootQuotaId, String makeQuotaDistrictId,List<KpiQuota> a,String taskYear) {
        DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(KpiQuota.class);
        detachedCriteria1.add(Restrictions.eq("parentQuotaId", rootQuotaId));
        detachedCriteria1.add(Restrictions.like("quotaScoringDepartId", makeQuotaDistrictId, MatchMode.ANYWHERE));
        detachedCriteria1.addOrder(Order.asc("quotaId"));
        List<KpiQuota> all1 = kpiQuotaService.findAll(detachedCriteria1);
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(KPIVillageQuota.class);
        Map<String,KpiQuota> kpiQuotaMap = all1.stream().collect(Collectors.toMap(KpiQuota::getQuotaId,k1->k1));
        Map<String,KpiQuota> kpiQuotaMap1 = a.stream().collect(Collectors.toMap(KpiQuota::getQuotaId,k->k));
        detachedCriteria.add(Restrictions.in("townQuotaId", all1.stream().map(KpiQuota::getQuotaId).collect(Collectors.toList())));
        detachedCriteria.add(Restrictions.eq("districtId", districtId));
        List<KPIVillageQuota> villageQuotas = findAll(detachedCriteria);
        Map<String, String> map = new HashMap<>();
        Map<String, List<Map<String, Object>>> thirdRes = new HashMap<>();
        for (KPIVillageQuota item : villageQuotas) {
            String parentQuotaId = item.getTownQuotaId();
            if (!thirdRes.containsKey(parentQuotaId)) {
                thirdRes.put(parentQuotaId, new ArrayList<>());
            }
            Map<String, Object> val = new HashMap<>();
            val.put("value", item.getScore());
            if (parentQuotaId.equals(taskYear +"0201")){
                DetectionIndexSearchable detectionIndexSearchable = new DetectionIndexSearchable();
                detectionIndexSearchable.setTaskId(reviewTaskId);
                detectionIndexSearchable.setDistrictId(districtId);
                List<DetectionIndex> all = detectionIndexService.findAll(detectionIndexSearchable);
                if (all.size() > 0) {
                    DetectionIndex detectionIndex = all.get(0);
                    val.put("villageLevelAchievementsScore",detectionIndex.getVillageLevelAchievementsScore());
                    val.put("villageLevelAchievements",detectionIndex.getVillageLevelAchievements());
                }

            }
            thirdRes.get(parentQuotaId).add(val);
        }
        List<Map<String, Object>> secondRes = new ArrayList<>();
        for (KPIVillageQuota item : villageQuotas) {
            Map<String, Object> val = new HashMap<>();
            val.put("quotasName",kpiQuotaMap.get(item.getTownQuotaId()).getQuotaName());
            val.put("quotaScore", kpiQuotaMap.get(item.getTownQuotaId()).getQuotaScore());
            val.put("attachment", kpiAttachmentService.findByQuota(item.getTownQuotaId(), item.getDistrictId(), null, reviewTaskId));
            val.put("thirdRes", thirdRes.get(item.getTownQuotaId()));
            val.put("villageQuota", item);
            secondRes.add(val);
        }

        return secondRes;
    }

    @Override
    public List<Map<String, Object>> buildWatchQuotaData(String districtId, String taskId, String reviewTaskId, String taskYear, String parentQuotaId, String makeQuotaDistrictId,List<KpiQuota> a) {
        DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(KpiQuota.class);
        detachedCriteria1.add(Restrictions.eq("parentQuotaId", parentQuotaId));
        detachedCriteria1.add(Restrictions.like("quotaScoringDepartId", makeQuotaDistrictId, MatchMode.ANYWHERE));
        detachedCriteria1.addOrder(Order.asc("quotaId"));

        List<KpiQuota> all1 = kpiQuotaService.findAll(detachedCriteria1);
        Map<String,KpiQuota> kpiQuotaMap = all1.stream().collect(Collectors.toMap(KpiQuota::getQuotaId,k1->k1));
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(KPIVillageQuota.class);
        detachedCriteria.add(Restrictions.in("townQuotaId", all1.stream().map(KpiQuota::getQuotaId).collect(Collectors.toList())));
        detachedCriteria.add(Restrictions.eq("districtId", districtId));
        List<KPIVillageQuota> villageQuotas = findAll(detachedCriteria);




        List<Map<String, Object>> secondRes = new ArrayList<>();
        for (KPIVillageQuota item : villageQuotas) {
            List<Map<String, Object>> thirdRes = new ArrayList<>();
            Map<String, Object> val = new HashMap<>();
            val.put("value", item.getScore());
            thirdRes.add(val);


            Map<String, Object> val1 = new HashMap<>();
            val1.put("quotasName",kpiQuotaMap.get(item.getTownQuotaId()).getQuotaName());
            val1.put("quotaScore", kpiQuotaMap.get(item.getTownQuotaId()).getQuotaScore());
            val1.put("attachment", kpiAttachmentService.findByQuota(item.getTownQuotaId(), item.getDistrictId(), null, reviewTaskId));
            val1.put("villageQuota", item);
            val1.put("thirdRes", handleAttachInfo(thirdRes,item.getParentQuotaId(),reviewTaskId,districtId,kpiQuotaMap.get(item.getTownQuotaId()).getQuotaName()));
            secondRes.add(val1);
        }

        return secondRes;
    }
    private List<Map<String, Object>> handleAttachInfo(List<Map<String, Object>> maps, @NonNull String parentQuotaId, @NonNull String taskId, @NonNull String districtId,String name) {
        KPIEvaluationSearchable kpiEvaluationSearchable = new KPIEvaluationSearchable();
        kpiEvaluationSearchable.setTaskId(taskId);
        kpiEvaluationSearchable.setDistrictId(districtId);

        DetectionIndexSearchable detectionIndexSearchable = new DetectionIndexSearchable();
        detectionIndexSearchable.setTaskId(taskId);
        detectionIndexSearchable.setDistrictId(districtId);
        List<DetectionIndex> all = detectionIndexService.findAll(detectionIndexSearchable);
        if (all.size() == 0) {
            return maps;
        }
        DetectionIndex detectionIndex = all.get(0);
        switch (name) {
            case "政治站位":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "政治站位等次");
                    put("value", detectionIndex.getField1());
                }});
                break;
            case "基础知识测试":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "基础知识测试得分");
                    put("value", detectionIndex.getField35());
                }});
                break;
            case "结构化面试测试":
                 maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "结构化面试测试得分");
                    put("value", detectionIndex.getField36());
                }});
                break;
            case "村级实绩":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村级实绩等次");
                    put("value", detectionIndex.getVillageLevelAchievements());
                }});
                 maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村级实绩得分");
                    put("value", detectionIndex.getVillageLevelAchievementsScore());
                }});
                break;
            case "日常表现":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "日常表现等次");
                    put("value", detectionIndex.getField2());
                }});
                break;
            case "为民实事工程建设":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "为民实事工程总额");
                    put("value", detectionIndex.getField3());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "年度村集体经济五项收入");
                    put("value", detectionIndex.getField4());
                }});
                break;
            case "年度村集体经济五项收入":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "本年度村集体经济五项收入");
                    put("value", detectionIndex.getField5());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "上年度村集体经济五项收入");
                    put("value", detectionIndex.getField6());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "上上年度村集体经济五项收入");
                    put("value", detectionIndex.getField7());
                }});
                break;
            case "乡村振兴重点任务":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "乡村振兴战略满意度调查得分");
                    put("value", detectionIndex.getField8());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "农村人居环境整治常态化管理");
                    put("value", detectionIndex.getField9());
                }});
                break;
            case "自然资源违法防控":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "违法行为处置");
                    put("value", detectionIndex.getField25());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "新增土地违法防控");
                    put("value", detectionIndex.getField26());
                }});
                break;
            case "农村党建重点工作":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "组织生活任务完成率得分");
                    put("value", detectionIndex.getPartyActivityFinishRatio());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村干部员额数量得分");
                    put("value", detectionIndex.getCadrePosts());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "为民服务标准化得分");
                    put("value", detectionIndex.getField31());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村级挂牌清理工作得分");
                    put("value", detectionIndex.getField32());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "网格巡查走访得分");
                    put("value", detectionIndex.getField33());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "乡风文明测评得分");
                    put("value", detectionIndex.getField34());
                }});
                break;
            case "信访矛盾调处化解":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村年度信访总量");
                    put("value", detectionIndex.getField27());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村级常住人口");
                    put("value", detectionIndex.getField28());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "赴镇江市及以上信访数量");
                    put("value", detectionIndex.getField29());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村年度信访化解数量");
                    put("value", detectionIndex.getField30());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "信访投诉率得分");
                    put("value", detectionIndex.getPetitionLetterAmount());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "越级上访率得分");
                    put("value", detectionIndex.getSkipPetitionLetterAmount());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "信访化解率得分");
                    put("value", detectionIndex.getDefusePetitionLetterAmount());
                }});
                break;
            case "综合评议":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "优秀");
                    put("value", detectionIndex.getField10());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "称职");
                    put("value", detectionIndex.getField11());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "基本称职");
                    put("value", detectionIndex.getField12());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "不称职");
                    put("value", detectionIndex.getField13());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "镇机关人员综合评议得分");
                    put("value", detectionIndex.getField14());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "优秀");
                    put("value", detectionIndex.getField15());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "称职");
                    put("value", detectionIndex.getField16());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "基本称职");
                    put("value", detectionIndex.getField17());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "不称职");
                    put("value", detectionIndex.getField18());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村“两委”干部测评得分");
                    put("value", detectionIndex.getField19());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "优秀");
                    put("value", detectionIndex.getField20());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "称职");
                    put("value", detectionIndex.getField21());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "基本称职");
                    put("value", detectionIndex.getField22());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "不称职");
                    put("value", detectionIndex.getField23());
                }});
               maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "党员群众满意度调查得分");
                    put("value", detectionIndex.getField24());
                }});
                break;
            case "行政能力研判":
                kpiEvaluationSearchable.setType("2");
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "行政能力（胜任力）评价表");
                    put("value", kpiEvaluationService.findAll(kpiEvaluationSearchable, new Sort(Sort.Direction.ASC, "indexNum")));
                }});
                break;
            case "加减分项":
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "村级实绩考核在全镇排名较上年度得分");
                    put("value", detectionIndex.getField27());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "获得国家、省、市、县涉农项目支持得分");
                    put("value", detectionIndex.getField28());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "重点工作或特色工作得分");
                    put("value", detectionIndex.getField29());
                }});
                maps.add(new HashMap<String, Object>(){{
                    put("isQuota", "0");
                    put("quotasName", "诫勉谈话或警告处分得分");
                    put("value", detectionIndex.getField30());
                }});
                break;
        }
        return maps;
    }
    @Override
    public List<Map<String, Object>> buildCommentQuotaData(String districtId, String taskId, String reviewTaskId,  String taskYear, String parentQuotaId, String makeQuotaDistrictId,List<KpiQuota> a) {
        DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(KpiQuota.class);
        detachedCriteria1.add(Restrictions.eq("parentQuotaId", parentQuotaId));
        detachedCriteria1.add(Restrictions.like("quotaScoringDepartId", makeQuotaDistrictId, MatchMode.ANYWHERE));
        detachedCriteria1.addOrder(Order.asc("quotaId"));

        List<KpiQuota> all1 = kpiQuotaService.findAll(detachedCriteria1);
        Map<String,KpiQuota> kpiQuotaMap = all1.stream().collect(Collectors.toMap(KpiQuota::getQuotaId,k1->k1));
        Map<String,KpiQuota> kpiQuotaMap1 = a.stream().collect(Collectors.toMap(KpiQuota::getQuotaId,k->k));
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(KPIVillageQuota.class);
        detachedCriteria.add(Restrictions.in("townQuotaId", all1.stream().map(KpiQuota::getQuotaId).collect(Collectors.toList())));
        detachedCriteria.add(Restrictions.eq("districtId", districtId));
        List<KPIVillageQuota> villageQuotas = findAll(detachedCriteria);
        Map<String, String> map = new HashMap<>();
        Map<String, List<Map<String, Object>>> thirdRes = new HashMap<>();
        for (KPIVillageQuota item : villageQuotas) {
            String parentQuotaId1 = item.getTownQuotaId();
            if (!thirdRes.containsKey(parentQuotaId1)) {
                thirdRes.put(parentQuotaId1, new ArrayList<>());
            }
            Map<String, Object> val = new HashMap<>();
            val.put("value", item.getScore());
            thirdRes.get(parentQuotaId1).add(val);
        }



        List<Map<String, Object>> secondRes = new ArrayList<>();
        for (KPIVillageQuota item : villageQuotas) {
            Map<String, Object> val = new HashMap<>();
            val.put("quotasName",kpiQuotaMap.get(item.getTownQuotaId()).getQuotaName());
            val.put("quotaScore", kpiQuotaMap.get(item.getTownQuotaId()).getQuotaScore());
            val.put("attachment", kpiAttachmentService.findByQuota(item.getTownQuotaId(), item.getDistrictId(), null, reviewTaskId));
            val.put("thirdRes", thirdRes.get(item.getTownQuotaId()));
            val.put("villageQuota", item);
            secondRes.add(val);
        }
        return secondRes;
    }

    @Override
    public void deleteAllByTownQuotaId(String townQuotaId) {
        kPIVillageQuotaRepository.deleteAllByTownQuotaId(townQuotaId);
    }

    @Override
    public void deleteAllByTownQuotaIdIn(List<String> townQuotaIds) {
        kPIVillageQuotaRepository.deleteAllByTownQuotaIdIn(townQuotaIds);
    }

    @Override
    public void updateScoreById(String score, String scoreEnd, String id) {
        kPIVillageQuotaRepository.updateScoreById(score, scoreEnd, id);
    }

    @Override
    public List<KPIVillageQuota> findAllByTownQuotaIdAndQuarter(@Nonnull String townQuotaId, @Nonnull String quarter) {
        return kPIVillageQuotaRepository.findAllByTownQuotaIdAndQuarterOrderByDistrictId(townQuotaId, quarter);
    }

    @Override
    public List<KPIVillageQuota> findAllByTownQuotaIdAndQuarterAndDistrictId(@Nonnull String townQuotaId, @Nonnull String quarter, @Nonnull String districtId) {
        return kPIVillageQuotaRepository.findAllByTownQuotaIdAndQuarterAndParentDistrictIdOrderByDistrictId(townQuotaId, quarter,districtId);
    }
    @Override
    public List<KPIVillageQuota> findAllByTownQuotaIdAndQuarterAndVillageDistrictId(@Nonnull String townQuotaId, @Nonnull String quarter, @Nonnull String districtId) {
        return kPIVillageQuotaRepository.findAllByTownQuotaIdAndQuarterAndDistrictIdOrderByDistrictId(townQuotaId, quarter,districtId);
    }
    @Override
    public List<KPIVillageQuota> findAllByParentQuotaId(String parentQuotaId) {
        return kPIVillageQuotaRepository.findAllByParentQuotaId(parentQuotaId);
    }

    @Override
    public Integer updateStateByTownQuotaIdAndParentDistrictId(Collection<String> townQuotaId, String parentDistrictId) {
        return kPIVillageQuotaRepository.updateStateByTownQuotaIdAndParentDistrictId(townQuotaId, parentDistrictId);
    }

    @Override
    public Integer updateStateByTownQuotaId(Collection<String> townQuotaId) {
        return kPIVillageQuotaRepository.updateStateByTownQuotaId(townQuotaId);
    }

    @Override
    public void addAll(List<KPIVillageQuota> quotas) {
        kPIVillageQuotaRepository.saveAll(quotas);
    }

    @Override
    public String exportAll(String[] rowName, List<Object[]> dataList) {
        HSSFWorkbook workbook = export("村书记年度考核汇总表",rowName,dataList);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        String url = null;
        try {
            workbook.write(os);
            ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
            url = fdfsService.uploadFile(is,Long.valueOf(is.available()),"村书记年度考核汇总表.xls");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

    @Override
    public Integer updateRejectListStatus(String parentDistrictId, String year) {
        return kPIVillageQuotaRepository.updateRejectListStatus(parentDistrictId, year);
    }

    @Override
    public Integer updateScore(String parentDistrictId, String year) {
        return kPIVillageQuotaRepository.updateScore(parentDistrictId, year);
    }

    @Override
    public Integer updateRejectListStatusDepartment(List<String> townQuotaId) {
        return kPIVillageQuotaRepository.updateRejectListStatusDepartment(townQuotaId);
    }


    private List<KPITownQuota> getKPITownQuota(String districtId, String makeQuotaDistrictId, String taskId, String parentQuotaId) {
        String townDistrictId = districtId.substring(0, 4);
        DetachedCriteria detachedCriteria1 = DetachedCriteria.forClass(KPITownQuota.class);
        detachedCriteria1.add(Restrictions.eq("districtId", townDistrictId));
        detachedCriteria1.add(Restrictions.eq("taskId", taskId));
        detachedCriteria1.add(Restrictions.like("parentQuotaId", parentQuotaId, MatchMode.START));
        detachedCriteria1.createAlias("kpiQuota", "k");
        detachedCriteria1.add(Restrictions.like("k.quotaScoringDepartId", makeQuotaDistrictId, MatchMode.ANYWHERE));
        return kpiTownQuotaService.findAll(detachedCriteria1);
    }

}
